from django.db import models
from django.contrib.auth.models import User

class Worker(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='workers')
    wno = models.CharField(max_length=10)
    name = models.CharField(max_length=50)
    position = models.CharField(max_length=50)
    gender = models.CharField(max_length=10)
    birth = models.DateField()
    phone = models.CharField(max_length=20)
    address = models.TextField()

    class Meta:
        unique_together = ['user', 'wno']  # 确保同一用户下工号唯一


class Notice(models.Model):
    NOTICE_TYPES = (
        ('system', '系统通知'),
        ('activity', '活动通知'),
        ('community', '厨小圈'),
    )

    title = models.CharField(max_length=50, verbose_name='标题')
    content = models.TextField(max_length=500, verbose_name='内容')
    notice_type = models.CharField(
        max_length=20,
        choices=NOTICE_TYPES,
        default='system',
        verbose_name='类型'
    )
    publisher = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='notices',
        verbose_name='发布人'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_active = models.BooleanField(default=True, verbose_name='是否有效')
    likes = models.ManyToManyField(User, related_name='liked_notices', blank=True, verbose_name='点赞用户')

    class Meta:
        verbose_name = '通知'
        verbose_name_plural = '通知列表'
        ordering = ['-created_at']

    def __str__(self):
        return self.title


class NoticeReadStatus(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='read_statuses')
    notice = models.ForeignKey(Notice, on_delete=models.CASCADE, related_name='read_statuses')
    is_read = models.BooleanField(default=False)
    read_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        unique_together = ('user', 'notice')
        verbose_name = '通知阅读状态'
        verbose_name_plural = '通知阅读状态'


class Comment(models.Model):
    notice = models.ForeignKey(
        Notice,
        on_delete=models.CASCADE,
        related_name='comments',
        verbose_name='所属通知'
    )
    content = models.TextField(max_length=500, verbose_name='评论内容')
    publisher = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='comments',
        verbose_name='评论人'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    likes = models.ManyToManyField(User, related_name='liked_comments', blank=True, verbose_name='点赞用户')

    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论列表'
        ordering = ['created_at']

    def __str__(self):
        return f"{self.publisher.username} 的评论"


class DingTalkRobot(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='dingtalk_robot')
    webhook = models.URLField(verbose_name='Webhook地址')
    secret = models.CharField(max_length=100, blank=True, null=True, verbose_name='加签密钥')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '钉钉机器人配置'
        verbose_name_plural = '钉钉机器人配置'

    def __str__(self):
        return f"{self.user.username}的钉钉机器人"